package com.seray.zebraprintlib.base;

import com.seray.zebraprintlib.config.FontType;
import com.seray.zebraprintlib.config.PrintConfig;
import com.seray.zebraprintlib.content.AZebraContent;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Author：李程
 * CreateTime：2018/8/9 09:28
 * E-mail：licheng@kedacom.com
 * Describe：
 */
public class BaseLabelBuilder<D> implements Builder<D> {

    /**
     * 数据源
     * 打印结果回调
     */
    private D data;

    /**
     * 标签宽度
     */
    private int width;

    /**
     * 标签高度
     */
    private int height;

    /**
     * 打印标签数量
     */
    private int count;

    /**
     * 行区域/字体大小
     */
    private int size;

    @FontType
    private String fontType;

    /**
     * 属性内容集合
     */
    private Map<String, AZebraContent> params;

    public BaseLabelBuilder() {
        this.params = new LinkedHashMap<>();
        this.width = PrintConfig.LABEL_WIDTH;
        this.height = PrintConfig.LABEL_HEIGHT;
        this.size = PrintConfig.NORMAL_TEXT_SIZE;
        this.count = PrintConfig.LABEL_SHEET;
        this.fontType = FontType.M002;
    }

    @Override
    public BaseLabel<D> build() {
        return new BaseLabel<>(this);
    }

    /**
     * 设置打印区域宽度
     *
     * @param width 打印区域宽度
     * @return 构造器
     */
    @Override
    public BaseLabelBuilder<D> width(int width) {
        this.width = width;
        return this;
    }

    /**
     * 设置打印区域高度
     *
     * @param height 打印区域高度
     * @return 构造器
     */
    @Override
    public BaseLabelBuilder<D> height(int height) {
        this.height = height;
        return this;
    }

    /**
     * 设置打印份数
     *
     * @param count 打印份数
     * @return 构造器
     */
    @Override
    public BaseLabelBuilder<D> count(int count) {
        this.count = count;
        return this;
    }

    @Override
    public BaseLabelBuilder<D> font(@FontType String fontType) {
        this.fontType = fontType;
        return this;
    }

    /**
     * 设置每行内容大小
     * 文本：字体大小；条码或二维码：码高及范围区域
     *
     * @param size 打印份数
     * @return 构造器
     */
    @Override
    public BaseLabelBuilder<D> size(int size) {
        this.size = size;
        return this;
    }

    /**
     * 添加正文内容
     *
     * @return 构造器
     */
    @Override
    public BaseLabelBuilder<D> param(AZebraContent content) {
        if (params != null) {
            String className = content.getClass().getSimpleName();
            int size = params.size();
            params.put(String.format("%s%s", className, size), content);
        }
        return this;
    }

    /**
     * 数据源
     * must be initialized
     *
     * @return 构造器
     */
    @Override
    public BaseLabelBuilder<D> data(D data) {
        this.data = data;
        return this;
    }

    public D getData() {
        return data;
    }

    public int getSize() {
        return size;
    }

    public int getWidth() {
        return width;
    }

    public int getHeight() {
        return height;
    }

    int getCount() {
        return count;
    }

    public String getFontType() {
        return fontType;
    }

    Map<String, AZebraContent> getParams() {
        return params;
    }

    @Override
    public String toString() {
        return "BaseLabelBuilder{" +
                "data=" + data +
                ", width=" + width +
                ", height=" + height +
                ", count=" + count +
                ", size=" + size +
                ", params=" + params +
                '}';
    }
}
